package com.leyou.goodspage.service;

import com.leyou.goodspage.client.BrandClient;
import com.leyou.goodspage.client.CategoryClient;
import com.leyou.goodspage.client.GoodsClient;
import com.leyou.goodspage.client.SpecificationClient;
import com.leyou.item.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsService {

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private CategoryClient categoryClient;

    @Autowired
    private BrandClient brandClient;

    @Autowired
    private SpecificationClient specificationClient;


    public Map<String, Object> loadModel(Long id) {

        try {
            //声明一个集合，来存放model模型数据
            Map<String, Object> modelMap = new HashMap<>();

            //查询spu的信息
            Spu spu = goodsClient.querySpuById(id);
            modelMap.put("spu", spu);

            //查询spuDetail的信息
            SpuDetail spuDetail = goodsClient.querySpuDetailById(id);
            modelMap.put("spuDetail", spuDetail);

            //查询sku的信息
            List<Sku> skus = goodsClient.querySkuBySpuId(id);
            modelMap.put("skus", skus);

            //查询商品的分类信息
            List<String> names = categoryClient.queryNameByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));
            System.out.println("names = " + names);
            //上面查出来的分类信息只有名字，但我们需要对象形式的
            Category category1 = new Category();
            category1.setId(spu.getCid1());
            category1.setName(names.get(0));

            Category category2 = new Category();
            category2.setId(spu.getCid2());
            category2.setName(names.get(1));

            Category category3 = new Category();
            category3.setId(spu.getCid3());
            category3.setName(names.get(2));
            List<Category> categories = Arrays.asList(category1, category2, category3);
            if (categories != null) {
                modelMap.put("categories",categories);
            }

            //查询品牌数据
            List<Brand> brands = brandClient.queryBrandByIds(Arrays.asList(spu.getBrandId()));
            modelMap.put("brand",brands.get(0));

            //查询规格参数组和规格参数信息
            List<SpecGroup> groups = specificationClient.querySpecsByCid(spu.getCid3());
            modelMap.put("groups",groups);

            //查询特有规格参数
            List<SpecParam> specParams = specificationClient.querySpecParam(null, spu.getCid3(), null, false);
            //处理成id：name的形式
            Map<Long,String> paramMap = new HashMap<>();
            for (SpecParam specParam : specParams) {
                paramMap.put(specParam.getId(),specParam.getName());
            }
            modelMap.put("paramMap",paramMap);

            //查询通用规格参数
            List<SpecParam> genParams = specificationClient.querySpecParam(null, spu.getCid3(), null, true);
            //处理成id：name的形式
            Map<Long,String> genparamMap = new HashMap<>();
            for (SpecParam genParam : genParams) {
                genparamMap.put(genParam.getId(),genParam.getName());
            }
            modelMap.put("genparamMap",genparamMap);


            //数据封装完毕后，返回
            return modelMap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
